package com.iims.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.exception.CustomException;
import com.iims.common.utils.SeqNumUtils;
import com.iims.finance.domain.CmisCwIncomeAffirm;
import com.iims.finance.enums.ReviewStatus;
import com.iims.finance.mapper.CmisCwIncomeAffirmMapper;
import com.iims.finance.service.ICmisCwIncomeAffirmService;
import com.iims.finance.vo.resp.CmisCwIncomeAffirmDetailVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/7/9 22:31
 */
@Service
public class CmisCwIncomeAffirmServiceImpl extends ServiceImpl<CmisCwIncomeAffirmMapper, CmisCwIncomeAffirm> implements ICmisCwIncomeAffirmService {

    @Override
    public List<CmisCwIncomeAffirmDetailVo> getCwIncomeAffirmList(CmisCwIncomeAffirm incomeAffirm) {
        List list = baseMapper.selectCwIncomeAffirmList(incomeAffirm);
        return SeqNumUtils.initSeqNum(incomeAffirm, list);
    }

    @Override
    public List<CmisCwIncomeAffirmDetailVo> getContractCwIncomeAffirmList(Long contractId) {
        return baseMapper.selectContractCwIncomeAffirmList(contractId);
    }

    @Override
    public CmisCwIncomeAffirmDetailVo getCwIncomeAffirmDetailById(Long proId) {
        return baseMapper.selectCwIncomeAffirmDetail(proId);
    }

    @Override
    public void insertCwIncomeAffirm(CmisCwIncomeAffirm incomeAffirm) {
        baseMapper.insert(incomeAffirm);
    }

    @Override
    public void updateCwIncomeAffirm(CmisCwIncomeAffirm incomeAffirm) {
        LambdaUpdateWrapper<CmisCwIncomeAffirm> updateWrapper = new LambdaUpdateWrapper<CmisCwIncomeAffirm>()
                .eq(CmisCwIncomeAffirm::getAffirmId, incomeAffirm.getAffirmId())
                .set(CmisCwIncomeAffirm::getAffirmTime, incomeAffirm.getAffirmTime())
                .set(CmisCwIncomeAffirm::getAffirmUser, incomeAffirm.getAffirmUser())
                .set(CmisCwIncomeAffirm::getContractId, incomeAffirm.getContractId())
                .set(CmisCwIncomeAffirm::getIncomeMoney, incomeAffirm.getIncomeMoney())
                .set(CmisCwIncomeAffirm::getPercentage, incomeAffirm.getPercentage())
                .set(CmisCwIncomeAffirm::getRemark, incomeAffirm.getRemark());
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteById(Long incomeAffirmId) {
        checkCmisCwIncomeAffirmUse(incomeAffirmId);
        baseMapper.deleteById(incomeAffirmId);
    }

    @Override
    public void checkCmisCwIncomeAffirmUse(Long incomeAffirmId) {

        CmisCwIncomeAffirm incomeAffirm = baseMapper.selectOne(new LambdaQueryWrapper<CmisCwIncomeAffirm>()
                .select(CmisCwIncomeAffirm::getAffirmId, CmisCwIncomeAffirm::getReviewStatus).eq(CmisCwIncomeAffirm::getAffirmId, incomeAffirmId));

        if (ReviewStatus.HasReviewed.getValue() == incomeAffirm.getReviewStatus().intValue()) {
            throw new CustomException("已审核的收入确认记录禁止删除!");
        }
    }
}
